package com.topdt.jbpm.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.thx.common.spring.BeanFactory;
import com.topdt.jbpm.dao.ActivityDao;
import com.topdt.jbpm.dao.ApplicationDao;
import com.topdt.jbpm.dao.CommentDao;
import com.topdt.jbpm.dao.ControlDao;
import com.topdt.jbpm.form.FormMenuDefin;
import com.topdt.jbpm.model.Activity;
import com.topdt.jbpm.model.Application;
import com.topdt.jbpm.model.Control;
import com.topdt.jbpm.model.WorkOpinion;

@Service
@Transactional(readOnly = true)
public class CommentService {
	@Autowired
	private CommentDao commentDao;

	@Autowired
	private ApplicationDao appDao;

	@Autowired
	private ControlDao ctlDao;

	@Autowired
	private ActivityDao actDao;

	@Transactional(readOnly = false)
	public void saveorUpdate(WorkOpinion work) {
		commentDao.save(work);
	}

	@Transactional(readOnly = false)
	public void update(WorkOpinion work) {
		commentDao.update(work);
	}

	/**
	 * 根据工作id取得意见列表 建立日期 : June 25, 2013 5:41:50 PM<br>
	 * 作者 : wangamin<br>
	 *
	 * @return
	 */
	public List<WorkOpinion> getWorkOPioionsByWorkId(String workId) {
		return commentDao.findByWorkId(workId);
	}

	/**
	 * 根据工作id 人员id取得意见列表 建立日期 : June 25, 2013 5:41:50 PM<br>
	 * 作者 : wangamin<br>
	 *
	 * @return
	 */
	public WorkOpinion getWorkOPioionsByTodoIdPid(String todoId, String personnelId) {
		return commentDao.getWorkOPioionsByTodoIdPid(todoId, personnelId);
	}

	/**
	 * 根据opinionid取得意见列表 建立日期 : June 25, 2013 5:41:50 PM<br>
	 * 作者 : wangamin<br>
	 *
	 * @return
	 */
	public WorkOpinion getWorkOPioionsByOpinionId(String opinionId) {
		// TODO Auto-generated method stub
		return commentDao.getWorkOPioionsByOpinionId(opinionId);
	}

	public List<WorkOpinion> findByOpinionName(String opinionName, String appId, String flowId, String workId) {
		return commentDao.findByOpinionName(opinionName, appId, flowId, workId);
	}

	public List<Object[]> getWorkOptionByWorkIdAndActivity(String workId, String activityName) {
		return commentDao.getWorkOptionByWorkIdAndActivity(workId, activityName);
	}

	@Transactional(readOnly = false)
	public void update(String opnId, String opnContent) {
		commentDao.update(opnId, opnContent);
	}

	/**
	 * 可以填写意见的环节
	 * @param appId
	 * @return
	 */
	public List<Activity> getOpnAct(String appId) {
		Application app = appDao.get(appId);
		String flowId = app.getFlowId();
		String flowKey = app.getFlowKey();
		List<Activity> list = actDao.getActivityByFlow(flowId, flowKey);

		List<Control> ctls = ctlDao.findContrlsByFlowIdAndAppId(flowId, appId);

		String[][] menus = FormMenuDefin.getMenuItems(app.getAppAttr());
		int mIndex = -1;
		for (String[] menu : menus) {
			mIndex++;
			if ("opinion".equals(menu[0]))
				break;
		}

		Map<String, Object> actsMap = new HashMap<String, Object>();

		if (mIndex == -1) {
			// 没有意见菜单
		} else {
			for (Control ctl : ctls) {
				String fCtl = ctl.getFunctionControl();
				if (fCtl.charAt(mIndex) == '1')
					actsMap.put(ctl.getActivityName(), new Object());
			}
		}

		List<Activity> acts = new ArrayList<Activity>();

		for (Activity act : list) {// 按顺序排列顺序
			String actName = act.getActivityName();
			if (actsMap.get(actName) != null)
				acts.add(act);
		}
		return acts;
	}
}